//
//  UIButton+Extension.swift
//  Weibo
//
//  Created by 李亚乐 on 15/12/6.
//  Copyright © 2015年 李亚乐. All rights reserved.
//

import UIKit

extension UIButton {
    /// 便利构造函数 前景&背景图片
    ///
    /// - parameter imageName:           图片名称
    /// - parameter backgroundImageName: 背景图片名称
    ///
    /// - returns:  UIButton
    convenience init(imageName: String, backgroundImageName: String?) {
        
        self.init()
        
        setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        setImage(UIImage(named: imageName + "_highlighted"), forState: UIControlState.Highlighted)
        
        //可选背景图片名称
        if let backgroundImageName = backgroundImageName {
        setBackgroundImage(UIImage(named: backgroundImageName), forState: UIControlState.Normal)
        setBackgroundImage(UIImage(named: backgroundImageName + "_highlighted"), forState: UIControlState.Highlighted)
        }
        
        sizeToFit()
    }
    
    /// 便利构造函数 背景图片 标题 颜色
    ///
    /// - parameter backgroundImageName: 背景图片名称
    /// - parameter title:               标题
    /// - parameter titleColor:          标题颜色
    ///
    /// - returns:  UIButton
    convenience init(backgroundImageName: String, title: String, titleColor: UIColor) {
        self.init()
        
        setBackgroundImage(UIImage(named: backgroundImageName), forState: UIControlState.Normal)
        setTitle(title, forState: UIControlState.Normal)
        setTitleColor(titleColor, forState: UIControlState.Normal)
        sizeToFit()
    }
    
    /// 便利构造函数 前景图片 标题
    ///
    /// - parameter title:      标题
    /// - parameter fontSize:   字体大小
    /// - parameter titleColor: 字体颜色
    /// - parameter imageName:  前景图片名称
    ///
    /// - returns:  UIButton
    convenience init(imageName: String? = nil, title: String, fontSize: CGFloat, titleColor: UIColor, backgroundcolor: UIColor? = nil) {
        self.init()
        
        setTitle(title, forState: .Normal)
        setTitleColor(titleColor, forState: .Normal)
        titleLabel?.font = UIFont.systemFontOfSize(fontSize)
        
        if let imageName = imageName {
            setImage(UIImage(named: imageName), forState: .Normal)
        }
        backgroundColor = backgroundcolor
        sizeToFit()
    }
}
